package com.ifinc.stocks.utils;

import java.math.BigDecimal;

/**
 * User: luotao
 * Date: 13-12-2
 * Time: 09:38
 */
public class RateUtils {


    public static double swap(double capital, double rate, int year, int month) {

        BigDecimal capital_big = round(capital, 3);
        BigDecimal rate_big = round(rate, 4);


        BigDecimal bigDecimal = rate_big.divide(new BigDecimal(month)).add(new BigDecimal(1));
        for (int i = 1; i < year * month; i++) {
            bigDecimal = bigDecimal.multiply(bigDecimal);
        }

        return bigDecimal.multiply(capital_big).doubleValue();
    }

    public static double swap1(double capital, double rate, int year, int month) {

        BigDecimal capital_big = round(capital, 3);
        BigDecimal rate_big = round(rate, 4);


        BigDecimal bigDecimal = rate_big.divide(new BigDecimal(month)).add(new BigDecimal(1));
        for (int i = 1; i < year * month; i++) {
            bigDecimal = bigDecimal.multiply(bigDecimal);
        }

        return bigDecimal.multiply(capital_big).doubleValue();
    }

    public static BigDecimal round(Number number, int round) {
        BigDecimal rate = new BigDecimal(number.doubleValue());
        BigDecimal bigDecimal = rate.setScale(round, BigDecimal.ROUND_HALF_UP);
        return bigDecimal;
    }
}
